package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Fitnesscheckins;
import com.ruoyi.system.service.IFitnesscheckinsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 健身打卡信息Controller
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/system/fitnesscheckins")
public class FitnesscheckinsController extends BaseController
{
    @Autowired
    private IFitnesscheckinsService fitnesscheckinsService;

    /**
     * 查询健身打卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:list')")
    @GetMapping("/list")
    public TableDataInfo list(Fitnesscheckins fitnesscheckins)
    {
        startPage();
        List<Fitnesscheckins> list = fitnesscheckinsService.selectFitnesscheckinsList(fitnesscheckins);
        return getDataTable(list);
    }

    /**
     * 导出健身打卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:export')")
    @Log(title = "健身打卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Fitnesscheckins fitnesscheckins)
    {
        List<Fitnesscheckins> list = fitnesscheckinsService.selectFitnesscheckinsList(fitnesscheckins);
        ExcelUtil<Fitnesscheckins> util = new ExcelUtil<Fitnesscheckins>(Fitnesscheckins.class);
        util.exportExcel(response, list, "健身打卡信息数据");
    }

    /**
     * 获取健身打卡信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:query')")
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") Long checkId)
    {
        return success(fitnesscheckinsService.selectFitnesscheckinsByCheckId(checkId));
    }

    /**
     * 新增健身打卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:add')")
    @Log(title = "健身打卡信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Fitnesscheckins fitnesscheckins)
    {
//        Date checkinsTime = new Date();
//        //设置打卡日期，为现在的时间
//        fitnesscheckins.setCheckDatetime(checkinsTime);
//        //获取当前登录用户名
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        startPage();
//        fitnesscheckins.setUserId(Long.valueOf(authentication.getName()));
//        int checkinsReult = fitnesscheckinsService.insertFitnesscheckinsDate(fitnesscheckins);
//        if(checkinsReult == 1){
//            return toAjax(fitnesscheckinsService.insertFitnesscheckins(fitnesscheckins));
//        }else{
//            return AjaxResult.error("已过日期，不允许打卡");
//        }
        return toAjax(fitnesscheckinsService.insertFitnesscheckins(fitnesscheckins));
    }

    /**
     * 修改健身打卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:edit')")
    @Log(title = "健身打卡信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Fitnesscheckins fitnesscheckins)
    {
        return toAjax(fitnesscheckinsService.updateFitnesscheckins(fitnesscheckins));
    }

    /**
     * 删除健身打卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:fitnesscheckins:remove')")
    @Log(title = "健身打卡信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkIds}")
    public AjaxResult remove(@PathVariable Long[] checkIds)
    {
        return toAjax(fitnesscheckinsService.deleteFitnesscheckinsByCheckIds(checkIds));
    }
}
